import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
import routes from './routes'
import api from './api'
import 'font-awesome/css/font-awesome.min.css'
import './mock/sample'
import axios from 'axios'

Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

Vue.prototype.$api = api
Vue.prototype.$http = axios

Vue.prototype.$fmtDate = function(date) {
  //年月日
  let yearStr = "" + date.getFullYear();
  let month = date.getMonth() + 1;
  let monthStr = (month < 10 ? "0" + month : "" + month);
  let day = date.getDate();
  let dayStr = (day < 10 ? "0" + day : "" + day);

  return yearStr + "-" + monthStr + "-" + dayStr;
}
// NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes,
  mode: 'history'
})

// router.beforeEach((to, from, next) => {
//   // NProgress.start();
//   if (to.path == '/login') {
//     sessionStorage.removeItem('user')
//   }
//   let user = JSON.parse(sessionStorage.getItem('user'))
//   if (!user && to.path != '/login') {
//     next({ path: '/login' })
//   } else {
//     next()
//   }
// })

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

axios.interceptors.response.use(res => {
  return res.data
}, err => {
  console.log(err.response)
  if (err.response != null) {
    if (err.response.status == 401) {
      router.replace({
        path: '/login',
        query: { redirect: router.currentRoute.fullPath }
      })
    } else if (err.response.status == 500) {
      Vue.prototype.$message({
        message: err.response.data.exception + "(" + err.response.data.path + ")",
        type: "error"
      })
      console.trace()
    } else {
      Vue.prototype.$message({
        message: err.response.statusText,
        type: "error"
      })
    }
  }
  return new Promise(() => { })
})
